import { _decorator, Component, director, Node } from 'cc';
import { SceneMgr } from '../../Framework/Scripts/Managers/SceneMgr';
import { GameApp } from '../../Game/Scripts/GameApp';
import { ResMgr } from '../../Framework/Scripts/Managers/ResMgr';
import { Debug } from '../../Framework/Scripts/Managers/Debug';
import { EventMgr } from '../../Framework/Scripts/Managers/EventMgr';
import { TimerMgr } from '../../Framework/Scripts/Managers/TimerMgr';
import { SoundMgr } from '../../Framework/Scripts/Managers/SoundMgr';
import { ExcelMgr } from '../../Framework/Scripts/Managers/ExcelMgr';
import { NodePoolMgr } from '../../Framework/Scripts/Managers/NodePoolMgr';
import { WsNetMgr } from '../../Framework/Scripts/Managers/WsNetMgr';
import { UIMgr } from '../../Framework/Scripts/Managers/UIMgr';

const { ccclass, property } = _decorator;

@ccclass('Boot')
export class Boot extends Component {    
    public static Instance: Boot = null;

    @property({
        tooltip:"是否开启DebugView 模块" 
    })
    private isDebug: boolean = false;

    @property({
        tooltip:"是否使用WebSocket" 
    })
    public useWebSocket: boolean = false;

    protected onLoad(): void {
        if(Boot.Instance !== null) {
            this.destroy();
            return;
        }
        Boot.Instance = this;

        //常驻节点
        director.addPersistRootNode(this.node);
        this.StartUp();
    }

    private async StartUp() {
        await this.CheckHotUpdate();
        await this.InitFramework();
    }

    private CheckHotUpdate() {

    }

    private async InitFramework() {
        // 资源加载模块
        this.node.addComponent(ResMgr).Init();
        // end 

        // 自定义事件订阅与发布模块
        this.node.addComponent(EventMgr).Init();
        // end 

        // UI框架
        this.node.addComponent(UIMgr).Init();
        // end 

        // Timer模块的初始化
        this.node.addComponent(TimerMgr).Init();
        // end

        // 初始化声音播放模块
        this.node.addComponent(SoundMgr).Init("Musics", "Sounds");
        // end

        // 初始化我们的Excel表格模块
        this.node.addComponent(ExcelMgr).Init();
        // end

        // 节点池模块
        this.node.addComponent(NodePoolMgr).Init();
        // end 

        // 场景管理模块
        this.node.addComponent(SceneMgr).Init();
        // end 

        // webSocket网络模块
        if(this.useWebSocket){
            this.node.addComponent(WsNetMgr).Init();
        }
        // end 

        // 初始化日志管理模块
        if(this.isDebug){
            await this.node.addComponent(Debug).Init();
        }
        // end 

        // 进入游戏
        this.node.addComponent(GameApp).Init();
        GameApp.Instance.EnterGame();
        // end 
    }

    protected update(dt: number): void {
        // console.log('=========Boot========');
    }
}